#! /usr/bin/env python
#
# Copyright 2008 Charles Connell <charles@connells.org>
#
# Recuerde is a simple list manager.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://gnu.org/licenses/.>
#

import wx
import recuerde
from recuerde import Item

class ItemPreviewPopup (wx.PopupTransientWindow):
    """Tooltip-like window that display all the properties of an item."""
    def __init__(self, parent, item, pos):
        """Create window."""
        wx.PopupTransientWindow.__init__(self, parent, wx.BORDER_NONE)

        # Move to position, probably the position of the mouse pointer
        self.Move(pos)

        # If the mouse leaves the window, call OnMouseLeave to close the window
        self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)

        # Create sizer to hold labels, 5 pixels between each
        sizer = wx.GridBagSizer(5, 5)
        self.SetSizer(sizer)

        # All text in this window is large
        self.SetWindowVariant(wx.WINDOW_VARIANT_LARGE)

        # Create label for the item's name
        itemName = wx.StaticText(self, label=item.GetName())
        # This label has large text (since the window already has large text, this is extra-large)
        itemName.SetWindowVariant(wx.WINDOW_VARIANT_LARGE)
        # Font is bold
        font = itemName.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        itemName.SetFont(font)
        # Add label to sizer
        sizer.Add(itemName, (0,0), (1,1), wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)

        # Create label for item's list's name and add it to the sizer
        listName = wx.StaticText(self, label= '(' + item.GetList().GetName() + ')')
        sizer.Add(listName, (0,1), (1,1), wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)

        # Add an empty row to the sizer
        sizer.AddStretchSpacer((1,0))
        sizer.AddStretchSpacer((1,1))

        # Initialize counter that know what row of the sizer we are currently on
        sizerRow = 2
        # Loop through fields in list
        for field in item.GetList().GetFields():
            # Create label with the field name and add it to the sizer
            fieldName = wx.StaticText(self, label=field[1])
            sizer.Add(fieldName, (sizerRow,0), (1,1), flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
            # If the field is of type date, convert it to a string, then throw it in a label
            if field[0] == recuerde.FIELDTYPE_DATE:
                property = wx.StaticText(self, label=item.GetProperty(field[1]).FormatDate())
            # Otherwise, just throw the string into a label
            else:
                property = wx.StaticText(self, label=item.GetProperty(field[1]))
            # Add the label to the sizer
            sizer.Add(property, (sizerRow,1), (1,1), flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL, border=5)
            # And move to the next row
            sizerRow += 1;

        # Fit the widgets in the window
        sizer.Fit(self)
        # Not really sure what this does differently from Fit()
        self.Layout()
        # Show the window
        self.Popup()

    def OnMouseLeave(self, event):
        """Mouse left window, so we disappear."""
        self.Destroy();


